package com.photoeditor.demo.util;

import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.widget.TextView;

import com.common.base.ComponentContext;

/**
 * Created by winn on 17/5/8.
 */

public class FontUtil {

    public final static Typeface CUSTOM_FONT = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/josefinsans-regular.ttf");
    public final static Typeface CUSTOM_FONT_BOLD = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/JosefinSans-Bold.ttf");
    public final static Typeface CUSTOM_FONT_HEAVY = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Avenir-Heavy.otf");
    public final static Typeface CUSTOM_FONT_MEDIUM = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Roboto-Medium.ttf");
    public final static Typeface CUSTOM_FONT_BLACK_ITALIC = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Roboto-BlackItalic.ttf");

    public final static Typeface AVENIR_FONT_HEAVY = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Avenir-Heavy.otf");
    public final static Typeface AVENIR_FONT_MEDIUM = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Avenir-Medium.otf");

    public final static Typeface LOBSTER = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Lobster.otf");

    public final static Typeface CUSTOM_FONT_CONDENSED = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Roboto-Condensed.ttf");

    public final static Typeface CUSTOM_FONT_BOLD_ITALIC = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Roboto-BoldItalic.ttf");

    public final static Typeface CUSTOM_FONT_MW_REGULAR = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Merriweather-Regular.ttf");

    public final static Typeface CUSTOM_FONT_MW_BOLD = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Merriweather-Bold.ttf");

    public final static Typeface CUSTOM_FONT_BLACK = Typeface.createFromAsset(ComponentContext.getContext().getAssets(),
            "fonts/Avenir-Black.otf");

    public static void setCustomFontMedium(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_MEDIUM);
        }
    }

    public static void setCustomFontMedium(TextView... view) {
        for (TextView tmp : view) {
            setCustomFontMedium(tmp);
        }
    }

    public static void setAvenirFontMedium(TextView view) {
        if (view != null) {
            view.setTypeface(AVENIR_FONT_MEDIUM);
        }
    }

    public static void setAvenirFontMedium(TextView... view) {
        for (TextView tmp : view) {
            setAvenirFontMedium(tmp);
        }
    }

    public static void setAvenirFontHeavy(TextView view) {
        if (view != null) {
            view.setTypeface(AVENIR_FONT_HEAVY);
        }
    }

    public static void setAvenirFontHeavy(TextView... view) {
        for (TextView tmp : view) {
            setAvenirFontHeavy(tmp);
        }
    }

    public static void setCustomFont(TextView view) {
        if (view != null) {
//            view.setTypeface(CUSTOM_FONT);
            view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
        }
    }

    public static void setCustomFontBold(TextView view) {
        if (view != null) {
//            view.setTypeface(CUSTOM_FONT_BOLD);
            view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
        }
    }

    public static void setCustomFontBold(TextView... view) {
        for (TextView tmp : view) {
            setCustomFontBold(tmp);
        }
    }

    public static void setCustomFont(TextView... view) {
        for (TextView tmp : view) {
            setCustomFont(tmp);
        }
    }

    public static void setCustomFontBlackItalic(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_BLACK_ITALIC);
        }
    }

    public static void setCustomFontLobster(TextView view) {
        if (view != null) {
            view.setTypeface(LOBSTER);
        }
    }

    public static void setAlertDialogCustomFont(AlertDialog dialog) {
        /*if (dialog == null) {
            return;
        }
        try {
            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object alertController = mAlert.get(dialog);

            Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");
            mTitleView.setAccessible(true);

            TextView title = (TextView) mTitleView.get(alertController);
            FontUtil.setCustomFont(title);

            Field mMessageView = alertController.getClass().getDeclaredField("mMessageView");
            mMessageView.setAccessible(true);

            TextView messageView = (TextView) mMessageView.get(alertController);
            FontUtil.setCustomFont(messageView);

            Field mButtonPositive = alertController.getClass().getDeclaredField("mButtonPositive");
            mButtonPositive.setAccessible(true);

            TextView buttonPositive = (TextView) mButtonPositive.get(alertController);
            FontUtil.setCustomFont(buttonPositive);

            Field mButtonNeutral = alertController.getClass().getDeclaredField("mButtonNeutral");
            mButtonNeutral.setAccessible(true);

            TextView buttonNeutral = (TextView) mButtonNeutral.get(alertController);
            FontUtil.setCustomFont(buttonNeutral);

            Field mButtonNegative = alertController.getClass().getDeclaredField("mButtonNegative");
            mButtonNegative.setAccessible(true);

            TextView buttonNegative = (TextView) mButtonNegative.get(alertController);
            FontUtil.setCustomFont(buttonNegative);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }*/
    }

    public static void setCustomFontMwRegular(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_MW_REGULAR);
        }
    }

    public static void setCustomFontHeavy(TextView... view) {
        for (TextView tmp : view) {
            setCustomFontHeavy(tmp);
        }
    }

    public static void setCustomFontHeavy(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_HEAVY);
        }
    }

    public static void setCustomFontMwBold(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_MW_BOLD);
        }
    }

    public static void setCustomFontBlack(TextView view) {
        if (view != null) {
            view.setTypeface(CUSTOM_FONT_BLACK);
        }
    }
}
